﻿using BatchPrint.Converter;
using BatchPrint.Models;
using BatchPrint.Services;
using BatchPrint.Windows;
using Microsoft.Extensions.DependencyInjection;
using System.Windows;
using Application = System.Windows.Application;

namespace BatchPrint;

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
    private readonly IServiceCollection _services;

    public IServiceProvider ServiceProvider { get; private set; }

    public new static App Current => (App)Application.Current;

    public App()
    {
        _services = new ServiceCollection();
        RegisterService(_services);
        ServiceProvider = _services.BuildServiceProvider();
    }

    private void RegisterService(IServiceCollection services)
    {
        services.AddSingleton<MainWindow>();
        services.AddSingleton<MainWindowViewModel>();

        services.AddTransient<ProgressWindow>();
        services.AddTransient<ProgressWindowViewModel>();

        services.AddSingleton<PrinterManager>();
        services.AddSingleton<ConverterProvider>();

        services.AddKeyedScoped<IImageConverter, PdfToImageConverter>(FileType.Pdf);
        services.AddKeyedScoped<IImageConverter, ExcelToImageConverter>(FileType.Excel);
        services.AddKeyedScoped<IImageConverter, ImageConverter>(FileType.Image);
    }

    protected override void OnStartup(StartupEventArgs e)
    {
        MainWindow = ServiceProvider.GetRequiredService<MainWindow>();
        MainWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
        MainWindow.Show();
    }
}
